<template>
    <div class="city-container">
        <Header :headerTitle="cityname" isShowBack>
            <template #changecity>
                <router-link to="/home" class="change-city"
                    >切换城市</router-link
                >
            </template>
        </Header>

        <form class="city-form" v-on:submit.prevent>
            <div>
                <input
                    type="search"
                    name="city"
                    placeholder="输入学校、商务楼、地址"
                    class="city-input input-style"
                    required
                    v-model="inputVaule"
                />
            </div>
            <div>
                <input
                    type="submit"
                    name="submit"
                    class="city-submit input-style"
                    @click="postpois"
                    value="提交"
                />
            </div>
        </form>

        <header v-if="historyTitle" class="pois-search-history">
            搜索历史
        </header>

        <ul class="getpois-ul">
            <li
                v-for="(item, index) in placeList"
                @click="nextpage(index, item.geohash)"
                :key="index"
            >
                <h4 class="pois-name ellipsis">{{ item.name }}</h4>
                <p class="pois-address ellipsis">{{ item.address }}</p>
            </li>
        </ul>

        <footer
            v-if="historyTitle && placeList.length"
            class="clear-all-history"
            @click="clearAll"
        >
            清空所有
        </footer>
        <div class="search-none-place" v-if="placeNone">很抱歉！无搜索结果</div>
    </div>
</template>

<script>
import { currentCity, searchPlace } from '@/api/city';
import { getItem, setItem, removeItem } from '@/utils/storage';

export default {
    data() {
        return {
            inputVaule: '', // 搜索地址
            cityId: '', // 当前城市id
            cityName: '', // 当前城市名字
            placeList:
                (getItem('placeHistory') &&
                    JSON.parse(getItem('placeHistory'))) ||
                [], // 搜索城市列表
            placeHistory: [], // 历史搜索记录
            historyTitle: true, // 默认显示搜索历史头部，点击搜索后隐藏
            placeNone: false // 搜索无结果，显示提示信息
        };
    },
    mounted() {
        this.getCityDetail();
    },
    methods: {
        async getCityDetail() {
            this.cityId = this.$route.params.id;
            //获取当前城市名字
            const res = await currentCity(this.cityId);
            this.cityName = res.name;
        },
        // 发送搜索信息 inputVaule
        async postpois() {
            //输入值不为空时才发送信息
            if (this.inputVaule) {
                const res = await searchplace(this.cityid, this.inputVaule);
                this.historyTitle = false;
                this.placeList = res;
                this.placeNone = res.length ? false : true;
            }
        },
        /**
         * 点击搜索结果进入下一页面时进行判断是否已经有一样的历史记录
         * 如果没有则新增，如果有则不做重复储存，判断完成后进入下一页
         */
        nextpage(index, geohash) {
            let history = getStore('placeHistory');
            let choosePlace = this.placeList[index];
            if (history) {
                let checkrepeat = false;
                this.placeHistory = JSON.parse(history);
                this.placeHistory.forEach(item => {
                    if (item.geohash == geohash) {
                        checkrepeat = true;
                    }
                });
                if (!checkrepeat) {
                    this.placeHistory.push(choosePlace);
                }
            } else {
                this.placeHistory.push(choosePlace);
            }
            setStore('placeHistory', this.placeHistory);
            this.$router.push({ path: '/msite', query: { geohash } });
        },
        clearAll() {
            removeStore('placeHistory');
            this.initData();
        }
    }
};
</script>

<style lang="scss" scoped>
@import 'src/style/mixin';
.city-container {
    padding-top: 2.35rem;
}
.change-city {
    right: 0.4rem;
    @include sc(0.6rem, #fff);
    @include ct;
}
.city-form {
    background-color: #fff;
    border-top: 1px solid $bc;
    border-bottom: 1px solid $bc;
    padding-top: 0.4rem;
    div {
        width: 90%;
        margin: 0 auto;
        text-align: center;
        .input-style {
            border-radius: 0.1rem;
            margin-bottom: 0.4rem;
            @include wh(100%, 1.4rem);
        }
        .city-input {
            border: 1px solid $bc;
            padding: 0 0.3rem;
            @include sc(0.65rem, #333);
        }
        .city-submit {
            background-color: $blue;
            @include sc(0.65rem, #fff);
        }
    }
}
.pois-search-history {
    border-top: 1px solid $bc;
    border-bottom: 1px solid $bc;
    padding-left: 0.5rem;
    @include font(0.475rem, 0.8rem);
}
.getpois-ul {
    background-color: #fff;
    border-top: 1px solid $bc;
    li {
        margin: 0 auto;
        padding-top: 0.65rem;
        border-bottom: 1px solid $bc;
        .pois-name {
            margin: 0 auto 0.35rem;
            width: 90%;
            @include sc(0.65rem, #333);
        }
        .pois-address {
            width: 90%;
            margin: 0 auto 0.55rem;
            @include sc(0.45rem, #999);
        }
    }
}
.search-none-place {
    margin: 0 auto;
    @include font(0.65rem, 1.75rem);
    color: #333;
    background-color: #fff;
    text-indent: 0.5rem;
}
.clear-all-history {
    @include sc(0.7rem, #666);
    text-align: center;
    line-height: 2rem;
    background-color: #fff;
}
</style>
